package com.hanbang.chu_xing_gao_zhao.view_me;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.application.MyApplication;
import com.hanbang.chu_xing_gao_zhao.base.BaseDataActivity;
import com.hanbang.chu_xing_gao_zhao.myview.MorePopupWindowPublic;
import com.hanbang.chu_xing_gao_zhao.myview.MorePopupWindowPublic.OnClickCallback;
import com.hanbang.chu_xing_gao_zhao.myview.MyDialog;
import com.hanbang.chu_xing_gao_zhao.myview.MyDialog.MODE;
import com.hanbang.chu_xing_gao_zhao.myview.RoundCornerImageView;
import com.hanbang.chu_xing_gao_zhao.utils.MyRequestCallBack;
import com.hanbang.cxgz_2.R;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * VIP 认证页面
 *
 * @author yang
 */
public class VIPUserAuthentication extends BaseDataActivity {
    /**
     * 肖像
     */
    private RoundCornerImageView portrait;

    /**
     * 导航按钮-年龄
     */
    private LinearLayout age;

    /**
     * 导航按钮-菜系
     */
    private LinearLayout styleOfCooking;

    /**
     * 导航按钮-职位
     */
    private LinearLayout position;

    /**
     * checkBox TextView map 对象
     */
    private Map<CheckBox, TextView> CTMap = new HashMap<CheckBox, TextView>();

    /**
     * checkBoxs组 实现单选
     */
    private ArrayList<CheckBox> checkBoxs = new ArrayList<CheckBox>();

    /**
     * 性别
     */
    private String gender = "null";

    /**
     * 拍完照存放的名称
     */
    private String camera = "capture.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vip_user_authentication);

        initView();
        setOnClickListener();
        addDataOnlySelect();

        requestCity();

    }

    /**
     * 初始化view
     */
    private void initView() {
        age = (LinearLayout) findViewById(R.id.vip_user_authentication_age);

        styleOfCooking = (LinearLayout) findViewById(R.id.ll_vip_user_authentication_styleOfCooking);
        position = (LinearLayout) findViewById(R.id.ll_vip_user_authentication_position);
        portrait = (RoundCornerImageView) findViewById(R.id.vip_user_authentication_portrait);
    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        age.setOnClickListener(onClickListener);

        styleOfCooking.setOnClickListener(onClickListener);
        position.setOnClickListener(onClickListener);
        portrait.setOnClickListener(onClickListener);

    }

    /**
     * 处理监听
     */
    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(final View v) {
            MorePopupWindowPublic popup = null;
            switch (v.getId()) {
                // 选择年龄
                case R.id.vip_user_authentication_age:
                    popup = new MorePopupWindowPublic(VIPUserAuthentication.this, R.layout.item_popupwindow_public,
                            ageArrayList, 8);
                    popup.showPopupWindow(age.getChildAt(0), 2);

                    break;

                // 选择菜系
                case R.id.ll_vip_user_authentication_styleOfCooking:
                    popup = new MorePopupWindowPublic(VIPUserAuthentication.this, R.layout.item_popupwindow_public,
                            styleOfCookingList, 8);
                    popup.showPopupWindow(styleOfCooking.getChildAt(0), 2);

                    break;
                // 选择职位
                case R.id.ll_vip_user_authentication_position:
                    popup = new MorePopupWindowPublic(VIPUserAuthentication.this, R.layout.item_popupwindow_public,
                            positionList, 8);
                    popup.showPopupWindow(position.getChildAt(0), 2);

                    break;

                // 更改头像
                case R.id.vip_user_authentication_portrait:
                    // if (isLogin(true)) {
                    // } else {
                    // break;
                    // }

                    // 新建一个dialog 对话框
                    MyDialog myDialog = new MyDialog(VIPUserAuthentication.this, R.style.about_me_dialog, MODE.HEAD);
                    // 添加图片的监听
                    myDialog.setOnClickCallback(new com.hanbang.chu_xing_gao_zhao.myview.MyDialog.OnClickCallback() {

                        @Override
                        public void onClick(MyDialog dialog, Object number, Object content) {

                            // 拍照
                            if ((Integer) number == 1) {
                                File fDir = new File(MyApplication.picFilepath);
                                if (fDir.exists() || fDir.mkdirs()) {
                                    File cameraFile = new File(fDir, camera);
                                    if (!cameraFile.exists()) {
                                        try {
                                            cameraFile.createNewFile();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
                                    startActivityForResult(intent, 998);
                                }
                                // 相册选择
                            } else if ((Integer) number == 2) {
                                // 获取相册
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                intent.putExtra("return-data", true);
                                startActivityForResult(intent, 999);
                            }

                        }
                    });
                    myDialog.show();

                    break;

                default:

                    break;
            }

            // 点击监听的处理
            if (popup != null) {

                popup.setOnClickCallback(new OnClickCallback() {

                    @Override
                    public void onClick(MorePopupWindowPublic morePopWindow, Object number, String content) {

                        for (int i = 0; i < morePopWindow.parentView.size(); i++) {
                            if ((Integer) number == i) {
                                // 先把v
                                // 转换成具体的容器的对象，然后再从容器对象得到第一个元素，并转换成TextView对象，然后进行赋值
                                ((TextView) ((LinearLayout) v).getChildAt(0)).setText(content);

                            }
                        }

                    }
                });
            }

        }
    };

    /**
     * intent 的回调方法
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 得到返回过来的城市
         */
        if (requestCode == 99 && resultCode == RESULT_OK) {

            String city = data.getStringExtra("city");

            ((TextView) findViewById(R.id.tv_register_perfect_city)).setText(city);

        }

    }

    ;

    /**
     * 添加单选数据
     */
    private void addDataOnlySelect() {
        CTMap.put((CheckBox) findViewById(R.id.vip_user_authentication_checkBox_man),
                (TextView) findViewById(R.id.vip_user_authentication_checkBox_man_text));
        CTMap.put((CheckBox) findViewById(R.id.vip_user_authentication_checkBox_women),
                (TextView) findViewById(R.id.vip_user_authentication_checkBox_women_text));

        checkBoxs.add((CheckBox) findViewById(R.id.vip_user_authentication_checkBox_man));
        checkBoxs.add((CheckBox) findViewById(R.id.vip_user_authentication_checkBox_women));

    }

    /**
     * checkBox 选择的监听一次只能选择一个选择，是单选按钮的功能
     *
     * @param v :是一个ViewGroup对象,所以要先转换成具体对象容器，然后在从容器中获取具体的对象
     */
    public void checkBoxListener(View v) {

        LinearLayout ll = (LinearLayout) v;

        for (int i = 0; i < checkBoxs.size(); i++) {

            // 把点击的控件设置为选中状态
            if (checkBoxs.get(i) == (CheckBox) ll.getChildAt(0)) {
                ((CheckBox) ll.getChildAt(0)).setSelected(true);

                // 职位类型赋值（从map 通过key中获取value）
                gender = CTMap.get((CheckBox) ll.getChildAt(0)).getText().toString();
            } else {
                checkBoxs.get(i).setSelected(false);
            }

        }
    }

    /**
     * 获取城市列表
     *
     * @throws IndexOutOfBoundsException
     */
    private void requestCity() throws IndexOutOfBoundsException {
        Log.i("text", "*000**" + "*/***");// *****ssss

        // 发送
        httpUtils.send(HttpMethod.GET, HttpInterfaces.verificationNumber + "&mobile=city",
                new MyRequestCallBack<String>(this, "正在获取城市列表...") {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        super.onSuccess(responseInfo);
                        try {
                            JSONObject json = new JSONObject(responseInfo.result);

                            int status = json.getInt("Result");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        // 设置顶部的标题
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));
        setTitleMore(View.VISIBLE, View.INVISIBLE, "VIP用户认证", null, null, null);

        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}
